package com.whp.openapi.web.controller;

import com.whp.openapi.common.ResponseResult;
import com.whp.openapi.common.ResultTable;
import com.whp.openapi.entity.Menu;
import com.whp.openapi.entity.Role;
import com.whp.openapi.service.MenuService;
import com.whp.openapi.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author whp
 * @date 2021/11/24
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    /**
     * 角色管理：搜索、分页
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @RequestMapping("/getRole")
    public ResultTable getRolePageWithName(@RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "10") Integer limit,
                                           String name) {
        ResultTable rt = roleService.getRolePageWithName(page, limit, name);
        return rt;
    }

    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    public ResponseResult updateStatus(Integer id, Integer status) {
        return roleService.updateStatus(id, status);
    }

    /**
     * 添加
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public ResponseResult add(Role role) {
        return roleService.addRole(role);
    }

    /**
     * 编辑（修改角色信息）
     * @param role
     * @return
     */
    @RequestMapping("/update")
    public ResponseResult update(Role role) {
        return roleService.updateRole(role);
    }
    
    //--------------授权----------------
    //角色拥有的所有权限
    @RequestMapping("/role_menu")
    public List<Integer> getRoleMenu(Integer roleId) {
        return roleService.getRoleMenu(roleId);
    }
    //权限树展示（菜单管理、角色管理、用户管理）
    @RequestMapping("/menu_tree")
    public List<Menu> getMenuTree() {
        return menuService.getAllMenuTree();
    }
    //修改权限
    @RequestMapping("/assign_menu")
    public ResponseResult assignMenu(Integer roleId, Integer[] menuIds){
        try {
            return roleService.assignMenu(roleId, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("操作失败！");
    }
}
